/**
 *
 */
package com.zhuoyue.zhtj.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.eit.eitsite.common.config.Global;
import com.eit.eitsite.common.persistence.Page;
import com.eit.eitsite.common.web.BaseController;
import com.eit.eitsite.common.utils.StringUtils;
import com.zhuoyue.zhtj.entity.Classroominfo;
import com.zhuoyue.zhtj.service.ClassroominfoService;

/**
 * 教室信息Controller
 * @author hss
 * @version 2017-09-26
 */
@Controller
@RequestMapping(value = "${adminPath}/zhtj/classroominfo")
public class ClassroominfoController extends BaseController {

	@Autowired
	private ClassroominfoService classroominfoService;
	
	@ModelAttribute
	public Classroominfo get(@RequestParam(required=false) String id) {
		Classroominfo entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = classroominfoService.get(id);
		}
		if (entity == null){
			entity = new Classroominfo();
		}
		return entity;
	}
	
	@RequiresPermissions("zhtj:classroominfo:view")
	@RequestMapping(value = {"list", ""})
	public String list(Classroominfo classroominfo, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Classroominfo> page = classroominfoService.findPage(new Page<Classroominfo>(request, response), classroominfo); 
		model.addAttribute("page", page);
		return "zhuoyue/zhtj/classroominfoList";
	}

	@RequiresPermissions("zhtj:classroominfo:view")
	@RequestMapping(value = "form")
	public String form(Classroominfo classroominfo, Model model) {
		model.addAttribute("classroominfo", classroominfo);
		return "zhuoyue/zhtj/classroominfoForm";
	}

	@RequiresPermissions("zhtj:classroominfo:edit")
	@RequestMapping(value = "save")
	public String save(Classroominfo classroominfo, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, classroominfo)){
			return form(classroominfo, model);
		}
		classroominfoService.save(classroominfo);
		addMessage(redirectAttributes, "保存教室信息成功");
		return "redirect:"+Global.getAdminPath()+"/zhtj/classroominfo/?repage";
	}
	
	@RequiresPermissions("zhtj:classroominfo:edit")
	@RequestMapping(value = "delete")
	public String delete(Classroominfo classroominfo, RedirectAttributes redirectAttributes) {
		classroominfoService.delete(classroominfo);
		addMessage(redirectAttributes, "删除教室信息成功");
		return "redirect:"+Global.getAdminPath()+"/zhtj/classroominfo/?repage";
	}

}